import unittest
import requests
from ddt import ddt, data, unpack


def convert_cny_to_usd(cny, exchange_rate=7.2):
    if cny < 0:
        raise ValueError("Amount in CNY cannot be negative")
    return cny / exchange_rate


'''
class TestUserRegistration(unittest.TestCase):
    """Test user registration with various username lengths"""

    def setUp(self):
        self.register_url = "https://swtesting-class-demo.koyeb.app/login/register"  # Replace with actual URL

    def test_username_too_long(self):
        """Test registration with username more than 8 characters"""
        response = requests.post(
            self.register_url, data={"username": "longusername", "password": "pass123"}
        )
        self.assertIn(b"Username must be between 5 and 8 characters", response.content)

    def test_username_too_short(self):
        """Test registration with username less than 5 characters"""
        response = requests.post(
            self.register_url, data={"username": "user", "password": "pass123"}
        )
        self.assertIn(b"Username must be between 5 and 8 characters", response.content)

    class TestMethods(unittest.TestCase):

        def test_cny_to_usd1(self):
            self.assertAlmostEqual(convert_cny_to_usd(816.7), 113.43, places=2)
'''


def is_prime(number):
    for i in range(2, number):
        if number % i == 0:
            return False
    return True


def area_rectangle(width, length):
    return width * length


@ddt
class Test_basic(unittest.TestCase):
    case1 = [3, 4, 12]

    @data(3, 5, 11, 13)
    def test_is_prime(self, value):
        self.assertTrue(is_prime(value))

    @data(6, 9, 12)
    def test_is_not_prime(self, value):
        self.assertFalse(is_prime(value))

    @data(case1, [5, 6, 30], [10, 20, 200])
    @unpack  # 注意拆包
    def test_rectangle_01(self, width, length, result):
        self.assertEqual(area_rectangle(width, length), result)

    @data(
        {"width": 20, "length": 6, "area": 120}, {"width": 30, "length": 6, "area": 180}
    )
    @unpack
    def test_rectangle_02(self, **data):
        self.assertEqual(area_rectangle(data["width"], data["length"]), data["area"])


if __name__ == "__main__":
    unittest.main()
